package tcp1;
/*
 * 服务器
 */
import java.io.*;
import java.net.*;

public class SerVerSocket1 {
	public static void main(String[] args) throws Exception{
			//创建一个获取客户端连接的serverSocket对象
			ServerSocket server=new ServerSocket(12654);
			System.out.println("ServerSocket对象创建成功");
			//通过serverSocket对象获取Socket套接字
			Socket socket =server.accept();
			System.out.println("客户端"+socket.getInetAddress().getHostAddress()+"连接成功");
			BufferedReader reader=new BufferedReader(new InputStreamReader
					(socket.getInputStream()));//读取输入流
			BufferedWriter writer=new BufferedWriter(new OutputStreamWriter
					(socket.getOutputStream()));//写出输出流
			String buffere=null;//用于储存字符的容器
			//读取从客户端发过来的数据并转换发送给客户端
			while((buffere=reader.readLine())!=null) {//读取文本行,无数据时为null即停止读取
				System.out.println(buffere);//将读取的字符打印到运行窗口
				if(buffere.equals("get over")) {//当读取的内容为指定字符时,跳出读取循环
					System.out.println("结束程序");
					break;
				}
				writer.write(buffere.toUpperCase());//将读取的内容转换成大写并写入输出流
				writer.newLine();//写入一个行分隔符
				writer.flush();//刷新输出流,让数据能够及时输出
			}
			
			reader.close();
			writer.close();
			socket.close();
			System.out.println("客户端"+socket.getInetAddress().getHostAddress()+"断开连接");
			
		}

}

